package gogogo.liwushuo.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import gogogo.liwushuo.R;
import gogogo.liwushuo.adapter.RecyclerAuthorAdapter;
import gogogo.liwushuo.adapter.RecyclerTitleAdapter;
import gogogo.liwushuo.bean.AuthorBean;
import gogogo.liwushuo.bean.TitleBean;
import gogogo.liwushuo.constant.Resource;
import gogogo.liwushuo.listener.OnItemClickListener;
import gogogo.liwushuo.util.OKHttpUtils;

/**
 * Created by chenjun on 2016/7/21.
 */
public class HomeTitleActivity extends AppCompatActivity {
    @InjectView(R.id.iv_title_top)
    ImageView ivTitleTop;
    @InjectView(R.id.tv_title_desc)
    TextView tvTitleDesc;
    @InjectView(R.id.back)
    ImageView back;
    @InjectView(R.id.tv_toolbar)
    TextView tvToolbar;
    @InjectView(R.id.toolbar_author)
    Toolbar toolbarAuthor;
    @InjectView(R.id.cospslayout)
    CollapsingToolbarLayout cospslayout;
    @InjectView(R.id.recyclerview)
    RecyclerView recyclerview;

    private Context mContext;
    private OKHttpUtils okHttpUtils;
    private int titleId;
    private String title;
    private List<TitleBean.DataBean.PostsBean> posts;
    private RecyclerTitleAdapter adapter;
    private TitleBean.DataBean dataBean;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_title);
        ButterKnife.inject(this);

        initData();
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
//        Log.i(Resource.TAG, "onResponse: "+posts.size());
        adapter = new RecyclerTitleAdapter(mContext, posts);
        recyclerview.setAdapter(adapter);
        getJsonData();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getJsonData() {

        okHttpUtils.getAsyncMainData(Resource.getTitleIntentUrl(titleId), new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }

            @Override
            public void onResponse(String result) {
                TitleBean titleBean = new Gson().fromJson(result, TitleBean.class);
                toolbarAuthor.setTitle("");
                tvToolbar.setText(title);
                dataBean = titleBean.getData();
                tvTitleDesc.setText(dataBean.getDescription());
                Picasso.with(mContext)
                        .load(dataBean.getCover_image_url())
                        .into(ivTitleTop);
                posts = dataBean.getPosts();
                adapter.seList(posts);
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClickListener(int position) {
                        int id = posts.get(position).getId();
                        Intent intent = new Intent(mContext,HomeImageActivity.class);
                        intent.putExtra("id",id);
                        startActivity(intent);
                    }
                });
            }
        });
    }

    private void initData() {
        mContext = this;
        Intent intent = getIntent();
        if (intent != null) {
            titleId = intent.getIntExtra("id", 0);
            title = intent.getStringExtra("title");
        }
        okHttpUtils = OKHttpUtils.newInstance(this);
        cospslayout.setExpandedTitleColor(0);
        cospslayout.setCollapsedTitleTextColor(Color.WHITE);
    }
}
